
import { useTableActionRedux } from "@/components/ant-ext-table/redux/HAdminContext";
import { useActionForm, useEdit } from "@/common/hooks";
import { Form} from "antd";
import HFormItem from "@/components/ant-ext/HFormItem";
import EnptList, { EnptListEntity } from "@/api/enpt-list";
import { useState, useEffect } from "react";
import HUpload from "@/components/ant-ext/HUpload";
import HDrawer from "@/components/ant-ext/HDrawer"
import LoginUser from "@/config/user";

export default function Index() {
    const [loading, setLoading] = useState(false)
    const { action, reset } = useTableActionRedux<EnptListEntity>()
    const { form, key, title } = useActionForm(action, [EnptList.EDIT], EnptList.MAPPING)
    const [actionUrl, setActionUrl] = useState('')
    const [formIsFileList, setFormIsFileList] = useState<string[]>([])
    const [disabled, setDisabled] = useState(false)

    const close = () => {
        reset()
        form.resetFields()
        setFormIsFileList([])
        setDisabled(false)
    }
    const saveAndFlush = useEdit(key === EnptList.EDIT ? EnptList.doEdit : EnptList.doSave, {
        setLoading: setLoading,
        success: () => {
        close()    
        }
    })


    useEffect(() => {
        setActionUrl('https://gyltest.chongxiaopai.com/gyl/file/fileResource/uploadFiles')
        if (action.match(EnptList.EDIT) || action.match(EnptList.DET)) {
            EnptList.detail({
                params: { id: action.getRow()?.id }, setLoading, success: (data) => {
                    if (action.match(EnptList.DET)) {
                        setDisabled(true)
                    }
                    form.resetFields()
                    form.setFieldsValue(data)
                    setFormIsFileList(form.getFieldValue("natureFile"))
                }
            })
        }
    }, [action, form, setFormIsFileList,setDisabled])



    return <>
        <HDrawer
            width={800}
            open={!!title}
            title={title}
            onClose={close}
        >
            <Form name={"editForm"} form={form} labelCol={{ span: 4 }}

onFieldsChange={(_, allFields) => {
    console.log(_,allFields)
  }}


                onFinish={() => {
                    saveAndFlush({ body: { ...form.getFieldsValue() } })
                }}
                
                
                
                >
                <HFormItem label={"id"} name={"id"} hidden={key === EnptList.ADD}
                    disabled={key !== EnptList.ADD} />
                <HFormItem disabled={disabled} label={"企业名称"} name={"companyName"} required />
                <HFormItem disabled={disabled} label={"企业类型"} name={"companyFlag"} type={"select"} dataSource={LoginUser.enptTypeToSelect} required />
                <HFormItem disabled={disabled} label={"企业法人"} name={"legalPerson"} required />
                <HFormItem disabled={disabled} label={"企业地址"} name={"address"} required />
                <HFormItem disabled={disabled} label={"联系人"} name={"person"} required />
                <HFormItem disabled={disabled} label={"联系方式"} name={"mobile"} required />
                <HFormItem disabled={disabled} label={"营业执照"} name={"natureFile"} required>
                    <HUpload name="natureFile"
                        transmission={(val: any) => {
                            let arr = val.map((p: any) => {
                                return p.url
                            })
                            setFormIsFileList(arr)
                        }}
                        formIsFileList={formIsFileList}
                        actionUrl={actionUrl}
                        filename={"natureFile"}
                        disabled={disabled}
                        maxCount={10} />
                </HFormItem>
            </Form>

            <button onClick={() => {
                form.setFieldValue('natureFile', formIsFileList)
                form.submit()
            }}>提交</button>
        </HDrawer>
    </>
}